package org.nuist.service.relation;

import org.nuist.pojo.Relation;
import org.nuist.pojo.User;
import org.nuist.pojo.VirtualMachine;
import org.nuist.repository.RelationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RelationServiceImpl implements RelationService{

    @Autowired
    RelationRepository relationRepository;

    @Override
    public void save(Relation relation) {
        relationRepository.save(relation);
    }

    @Override
    public void delete(Relation relation) {
        relationRepository.delete(relation);
    }

    @Override
    public List<Relation> findAll() {
        return relationRepository.findAll();
    }

    @Override
    public Relation findRelationById(Integer id) {
        return relationRepository.findRelationById(id);
    }

    @Override
    public void modifyRelation(Relation relation) {
        int relationId=relation.getId();
        Relation oldRelation=relationRepository.findRelationById(relationId);
        User user=relation.getUser();
        VirtualMachine vm=relation.getVirtualMachine();
        oldRelation.setUser(user);
        oldRelation.setVirtualMachine(vm);
        relationRepository.save(oldRelation);
    }

    @Override
    public User findUserByRelationId(Integer id) {
        return relationRepository.findRelationById(id).getUser();
    }

    @Override
    public VirtualMachine findVirtualMachineByRelationId(Integer id) {
        return relationRepository.findRelationById(id).getVirtualMachine();
    }

    @Override
    public Integer hasUser(Integer id) {
        return relationRepository.hasUser(id);
    }

    @Override
    public Integer hasVM(Integer id) {
        return relationRepository.hasVM(id);
    }

    @Override
    public Relation findRelationByUserId(Integer id) {
        return relationRepository.findRelationByUserId(id);
    }

    @Override
    public Relation findRelationByVMId(Integer id) {
        return relationRepository.findRelationByVMId(id);
    }


}
